﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FO.Models;
using FOSystem.CultureHelper;
namespace FO.Controllers
{
    public class FileController : CultureController
    {
        //
        // GET: /File/

        [Authorize]
        public ActionResult Index()
        {
            string serverPath = Server.MapPath("~/Content");
            DirectoryInfo d = new DirectoryInfo(serverPath);
            List<SpeFolder> listf = new List<SpeFolder>();
            foreach (DirectoryInfo ff in d.GetDirectories())
            {
                SpeFolder a = new SpeFolder();
                a.FullName = ff.FullName;
                a.Name = ff.Name;
                if (ff.GetFiles().Count() > 0 || ff.GetDirectories().Count() > 0)
                    a.hasFiles = true;
                listf.Add(a);
            }
            return View(listf);
        }
        public JsonResult ReadFolder(string folderPath)
        {
            string serverPath = Server.MapPath("~/" + folderPath);
            DirectoryInfo d = new DirectoryInfo(serverPath);
            List<SpeFolder> listf = new List<SpeFolder>();
            foreach (DirectoryInfo ff in d.GetDirectories())
            {
                SpeFolder a = new SpeFolder();
                a.FullName = ff.FullName;
                a.Name = ff.Name;
                if (ff.GetFiles().Count() > 0 || ff.GetDirectories().Count() > 0)
                    a.hasFiles = true;
                listf.Add(a);
            }
     
            return Json(listf, JsonRequestBehavior.AllowGet);
        }
        public JsonResult ReadFiles(string folderPath)
        {
            string serverPath = Server.MapPath("~/" + folderPath);
            DirectoryInfo d = new DirectoryInfo(serverPath);
            List<SpeFile> listf = new List<SpeFile>();
            foreach (FileInfo ff in d.GetFiles())
            {
                SpeFile a = new SpeFile();
                a.FullName = ff.FullName;
                a.Name = ff.Name;
                a.Size = ff.Length;
                a.isImage = isImage(a.FullName);
                a.Extention = ff.Extension;
                listf.Add(a);
            }
          
            return Json(listf, JsonRequestBehavior.AllowGet);
        }
        private bool isImage(string spath)
        {
            bool isImage;
            try
            {
                Bitmap.FromFile(spath);
                isImage = true;
            }
            catch
            {
                isImage = false;
            }
            return isImage;
        }

        public JsonResult ajaxDeleteFile(string spath)
        {
            string serverPath = Server.MapPath("~/" + spath);
            FileInfo fi = new FileInfo(serverPath);
          
            if (fi.Exists)
            {
                if (fi.Extension == ".js" || fi.Extension == ".css")
                    return Json(new { msg = "You dont have permission to delete this file!", status = false }, JsonRequestBehavior.AllowGet);
                try
                {
                    fi.Delete();
                }
                catch
                {
                    return Json(new { msg = "Can't delete this file!", status = false }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { status = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { msg = "File don't exists!", status = false }, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult ajaxRenameFile(string spath, string foder, string newfilename)
        {
            string serverPath = Server.MapPath("~/" + spath);
            FileInfo fi = new FileInfo(serverPath);
            if (fi.Exists)
            {

                try
                {
                    newfilename = newfilename.Trim().Replace(" ", "-");
                    string movepath = Server.MapPath("~/" + foder + "/" + newfilename);
                    fi.MoveTo(movepath);

                }
                catch
                {
                    return Json(new { msg = "Can't rename this file!", status = false }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { status = true }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { msg = "File don't exists!", status = false }, JsonRequestBehavior.AllowGet);
            }
        }
        [HttpPost]
        public ActionResult Upload(string qqFile, string FolderPath)
        {
            // The upload action will be called by the client control
            // for each file that was selected by the user for upload

            var path = Server.MapPath("~/" + FolderPath);

            var file = Path.Combine(path, qqFile);
            using (var output = System.IO.File.Create(file))
            {
                Request.InputStream.CopyTo(output);
            }
            return Json(new { success = true });
        }


    }
}
